package com.qiyi.player.core.model.impls
{
   import flash.events.EventDispatcher;
   import com.qiyi.player.core.model.IMovieInfo;
   import com.qiyi.player.core.IDestroy;
   import com.qiyi.player.core.model.impls.subtitle.Language;
   import com.qiyi.player.base.pub.EnumItem;
   import flash.utils.Timer;
   import com.qiyi.player.core.model.remote.RequestVRSInfoRemote;
   import com.qiyi.player.core.player.coreplayer.ICorePlayer;
   import com.qiyi.player.base.rpc.RemoteObjectEvent;
   import flash.events.TimerEvent;
   import com.qiyi.player.core.model.events.MovieEvent;
   import com.qiyi.player.base.rpc.RemoteObjectStatusEnum;
   import com.qiyi.player.core.model.utils.ErrorCodeUtils;
   import com.adobe.serialization.json.JSON;
   import com.qiyi.player.base.utils.Utility;
   import com.qiyi.player.core.model.def.ChannelEnum;
   import com.qiyi.player.core.model.def.LanguageEnum;
   
   public class MovieInfo extends EventDispatcher implements IMovieInfo, IDestroy
   {
      
      public function MovieInfo(param1:ICorePlayer) {
         super();
         this._ready = false;
         this._pingBackFlag = false;
         this._channel = ChannelEnum.NONE;
         this._pageUrl = "";
         this._ptUrl = "";
         this._title = "";
         this._albumName = "";
         this._albumUrl = "";
         this._nextUrl = "";
         this._subTitle = "";
         this._holder = param1;
      }
      
      private var _info:String;
      
      private var _infoJSON:Object;
      
      private var _subtitles:Vector.<Language>;
      
      private var _chains:Array;
      
      private var _defaultSubtitle:Language;
      
      private var _focusTips:Vector.<FocusTip>;
      
      private var _channel:EnumItem;
      
      private var _pageUrl:String;
      
      private var _title:String;
      
      private var _albumName:String;
      
      private var _albumUrl:String;
      
      private var _allSet:int;
      
      private var _allowDownload:Boolean;
      
      private var _nextUrl:String;
      
      private var _subTitle:String;
      
      private var _ptUrl:String;
      
      private var _isShare:Boolean;
      
      private var _isFullShare:Boolean;
      
      private var _ready:Boolean;
      
      private var _timer:Timer;
      
      private var _loader:RequestVRSInfoRemote;
      
      private var _pingBackFlag:Boolean;
      
      private var _holder:ICorePlayer;
      
      public function get info() : String {
         return this._info;
      }
      
      public function set info(param1:String) : void {
         this._info = param1;
         this.parse();
      }
      
      public function get infoJSON() : Object {
         return this._infoJSON;
      }
      
      public function get share() : Boolean {
         return this._isShare;
      }
      
      public function get fullShare() : Boolean {
         return this._isFullShare;
      }
      
      public function get subtitles() : Vector.<Language> {
         return this._subtitles;
      }
      
      public function get subTitle() : String {
         return this._subTitle;
      }
      
      public function get chains() : Array {
         return this._chains;
      }
      
      public function get defaultSubtitle() : Language {
         return this._defaultSubtitle;
      }
      
      public function get focusTips() : Vector.<FocusTip> {
         return this._focusTips;
      }
      
      public function get channel() : EnumItem {
         return this._channel;
      }
      
      public function get pageUrl() : String {
         return this._pageUrl;
      }
      
      public function get ptUrl() : String {
         return this._ptUrl;
      }
      
      public function get title() : String {
         return this._title;
      }
      
      public function get albumName() : String {
         return this._albumName;
      }
      
      public function get albumUrl() : String {
         return this._albumUrl;
      }
      
      public function get allSet() : int {
         return this._allSet;
      }
      
      public function get allowDownload() : Boolean {
         return this._allowDownload;
      }
      
      public function get nextUrl() : String {
         return this._nextUrl;
      }
      
      public function get ready() : Boolean {
         return this._ready;
      }
      
      public function startLoadInfo() : void {
         if(this._info == null || this._info == "")
         {
            this.reLoad();
         }
      }
      
      public function startInitByInfo(param1:String) : void {
         if(this._info == null || this._info == "")
         {
            if(this._loader)
            {
               this._loader.removeEventListener(RemoteObjectEvent.Evt_StatusChanged,this.onStatusChanged);
               this._loader.destroy();
               this._loader = null;
            }
            if(this._timer)
            {
               this._timer.removeEventListener(TimerEvent.TIMER,this.onTimer);
               this._timer.stop();
               this._timer = null;
            }
            this._info = param1;
            this.parse();
            this._ready = true;
            dispatchEvent(new MovieEvent(MovieEvent.Evt_Ready));
         }
      }
      
      private function reLoad() : void {
         if(this._loader)
         {
            this._loader.removeEventListener(RemoteObjectEvent.Evt_StatusChanged,this.onStatusChanged);
            this._loader.destroy();
         }
         this._loader = new RequestVRSInfoRemote(this._holder);
         this._loader.addEventListener(RemoteObjectEvent.Evt_StatusChanged,this.onStatusChanged);
         this._loader.initialize();
      }
      
      private function onStatusChanged(param1:RemoteObjectEvent) : void {
         var _loc2_:* = 0;
         if(this._timer)
         {
            this._timer.removeEventListener(TimerEvent.TIMER,this.onTimer);
            this._timer.stop();
            this._timer = null;
         }
         if(this._loader.status == RemoteObjectStatusEnum.Success)
         {
            this._info = this._loader.getData() as String;
            this.parse();
            this._ready = true;
            dispatchEvent(new MovieEvent(MovieEvent.Evt_Ready));
         }
         else if(this._loader.status != RemoteObjectStatusEnum.Processing)
         {
            _loc2_ = ErrorCodeUtils.getErrorCodeByRemoteObject(this._loader,this._loader.status);
            if((this._holder) && !this._pingBackFlag)
            {
               this._holder.pingBack.sendError(_loc2_);
               this._pingBackFlag = true;
            }
            this._timer = new Timer(5000,1);
            this._timer.addEventListener(TimerEvent.TIMER,this.onTimer);
            this._timer.start();
         }
         
         this._loader.removeEventListener(RemoteObjectEvent.Evt_StatusChanged,this.onStatusChanged);
         this._loader.destroy();
         this._loader = null;
      }
      
      private function onTimer(param1:TimerEvent) : void {
         this.reLoad();
      }
      
      private function parse() : void {
         var _loc5_:Language = null;
         var _loc6_:FocusTip = null;
         if(this._info == null || this._info == "")
         {
            return;
         }
         var _loc1_:Object = com.adobe.serialization.json.JSON.decode(this._info);
         this._channel = Utility.getItemById(ChannelEnum.ITEMS,int(_loc1_.c));
         this._pageUrl = _loc1_.vu.toString();
         this._title = _loc1_.vn.toString();
         this._albumName = _loc1_.an.toString();
         this._albumUrl = _loc1_.au.toString();
         this._allSet = int(_loc1_.es.toString());
         this._nextUrl = _loc1_.nurl.toString();
         this._subTitle = _loc1_.subt.toString();
         this._isShare = int(_loc1_["is"]) == 1;
         this._isFullShare = int(_loc1_.ifs) == 1;
         this._ptUrl = _loc1_.pturl.toString();
         this._allowDownload = !(int(_loc1_.idl) == 0);
         this._infoJSON = _loc1_;
         this._subtitles = new Vector.<Language>();
         var _loc2_:Array = _loc1_.stl.stl as Array;
         var _loc3_:String = _loc1_.stl.d.toString();
         var _loc4_:Object = null;
         for each(_loc4_ in _loc2_)
         {
            _loc5_ = new Language();
            _loc5_.isDefault = int(_loc4_.pre) == 1;
            _loc5_.url = _loc3_ + _loc4_.l.toString();
            _loc5_.lang = Utility.getItemById(LanguageEnum.ITEMS,int(_loc4_.lid));
            this._subtitles.push(_loc5_);
            if(_loc5_.isDefault)
            {
               this._defaultSubtitle = _loc5_;
            }
         }
         this._focusTips = new Vector.<FocusTip>();
         _loc2_ = _loc1_.fl as Array;
         for each(_loc4_ in _loc2_)
         {
            _loc6_ = new FocusTip();
            _loc6_.index = this._focusTips.length;
            _loc6_.timestamp = Number(_loc4_.t.toString()) * 1000;
            _loc6_.content = _loc4_.c.toString();
            this._focusTips.push(_loc6_);
         }
         if(_loc1_.tpl is Array)
         {
            this._chains = _loc1_.tpl;
         }
      }
      
      public function destroy() : void {
         if(this._timer)
         {
            this._timer.removeEventListener(TimerEvent.TIMER,this.onTimer);
            this._timer.stop();
            this._timer = null;
         }
         if(this._loader)
         {
            this._loader.removeEventListener(RemoteObjectEvent.Evt_StatusChanged,this.onStatusChanged);
            this._loader.destroy();
         }
         this._info = null;
         this._infoJSON = null;
         this._channel = ChannelEnum.NONE;
         this._subtitles = null;
         this._chains = null;
         this._defaultSubtitle = null;
         this._focusTips = null;
         this._ready = false;
         this._pingBackFlag = false;
         this._holder = null;
      }
   }
}
